package com.tnl.web.service;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class SMSService {
	private static final Logger logger = LoggerFactory.getLogger(SMSService.class);
	public static final String smsYRCUid="medtalent";
	public static final String smsYRCKey="c8d3a6d07b16580f1251";

	public static String sendSMS(String phoneNum,String text) {
		String result = null;
		try {
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
			post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");

			NameValuePair[] data = { new NameValuePair("Uid", smsYRCUid),
					new NameValuePair("Key", smsYRCKey),
					new NameValuePair("smsMob", phoneNum),
					new NameValuePair("smsText", text)
			};

			post.setRequestBody(data);

			client.executeMethod(post);
			Header[] headers = post.getResponseHeaders();
			int statusCode = post.getStatusCode();
			logger.debug("statusCode:" + statusCode);
			for (Header h : headers) {
				logger.debug(h.toString());
			}
			result = new String(post.getResponseBodyAsString().getBytes("gbk"));

			logger.debug("result" + result);
			post.releaseConnection();
		}  catch (Exception e) {
			logger.error("【后台认证平台】 医人才账号发送短信异常：" + e.getMessage());
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
//		sendSMS("13584593495","Asc1");
	}
}